package ui;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.FileInputStream;

/*
关于游戏设置的窗口
 */
public class SetFrame extends JFrame{
    //单选按钮
    JRadioButton b1 = new JRadioButton("打开音乐");
    JRadioButton b2 = new JRadioButton("关闭音乐");
    JButton b3 = new JButton("返回主菜单");
    JLabel label = new JLabel("背景音乐:");
    StartFrame frame;
    Thread playing;
    //单选按钮组
    public SetFrame(StartFrame frame){
        this.frame = frame;
        setTitle("游戏设置");
        setSize(800,600);
        setLocationRelativeTo(null);//设置窗口居中
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(null);
        //添加按钮
        ButtonGroup group = new ButtonGroup();
        group.add(b1);
        group.add(b2);
        //设置标签
        label.setFont(new Font("微软雅黑",Font.BOLD,25));
        label.setBounds(30,30,200,50);
        //添加组件
        panel.setBackground(Color.PINK);
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(label);
        b1.setBounds(200,30,100,50);
        b2.setBounds(300,30,100,50);
        b3.setBounds(250,100,100,50);
        this.add(panel);
        //注册监听
        b1.addActionListener(new ButListener());
        b2.addActionListener(new ButListener());
        b3.addActionListener(new ButListener());
        setVisible(true);
    }
    class ButListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            AbstractButton but = (AbstractButton) e.getSource();
            if (but.getText().equals("打开音乐")){
                System.out.println("音乐已打开");
                //开始播放音乐的线程
                playing = new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            BufferedInputStream buffer = new BufferedInputStream(new FileInputStream("F:\\IDEA项目库\\小球游戏\\src\\music\\test2.mp3"));
                            System.out.println("正在解码....");
                            Player player = null;
                            try {
                                player = new Player(buffer);
                                System.out.println("音乐开始播放");
                                
                                player.play();//播放
                            } catch (JavaLayerException e) {
                                e.printStackTrace();
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                };
                playing.start();
            }else if (but.getText().equals("关闭音乐")){
                System.out.println("关闭音乐");
                playing.stop();
            }else if (but.getText().equals("返回主菜单")){
                frame.setVisible(true);
                dispose();//关闭当前窗口
            }
        }
    }
}
